class CampaignsController < WebappController
  
  before_filter :ensure_authenticated, :only=>[:join_page, :join]
  #ensure_authenticated_to_facebook :only=>[:join]
  
  
  #Retrieve all active campaigns
  def index
    @campaigns = Campaign.active_campaigns.paginate(:page => params[:page], :per_page=>WebappConstants::RECORDS_PER_PAGE)
  end
  
  
  def ended
    @campaigns = Campaign.ended_campaigns.paginate(:page => params[:page], :per_page=>WebappConstants::RECORDS_PER_PAGE)
  end
  def upcoming
    @campaigns = Campaign.next_starting_campaigns.paginate(:page => params[:page], :per_page=>WebappConstants::RECORDS_PER_PAGE)
  end
  
  def campaigns_for_user
    @user = User.find(params[:user_id])
    @campaigns = Campaign.campaigns_for_user(@user.id).paginate(:page => params[:page], :per_page=>WebappConstants::RECORDS_PER_PAGE)  

  end
  
  def show
    @campaign = Campaign.find(params[:id])
  end
  
  def participants
    @campaign = Campaign.find(params[:id])
    @user_campaigns = @campaign.user_campaigns.paginate(:page => params[:page], :per_page=>WebappConstants::RECORDS_PER_PAGE)
  end
  
  def all_participants
    @campaign = Campaign.find(params[:id])
    @user_campaigns = @campaign.all_user_campaigns.paginate(:page => params[:page], :per_page=>WebappConstants::RECORDS_PER_PAGE)
    render :participants
  end
  
  def join_page
    #the campaign the user want to participate
    @campaign = Campaign.find(params[:id])
    #find the campaign the user is participating
    @email = ""
    @current_campaign = @current_user.active_campaign
    @accept_terms = true
  end
  
  def join
    @campaign = Campaign.find(params[:id])
    if @campaign.active?
      @accept_terms = params[:accept_terms]
      @email = params[:email]
      
      if @accept_terms
        if !@current_user.is_email_set && !@email
          flash_error(:campaign_requires_email)
          render :join_page
        else
          if !@current_user.is_email_set && @email 
            @current_user.email = @email
            raise ActiveRecord::ActiveRecordError unless @current_user.valid?
            @current_user.is_email_set=true
            @current_user.save!
          end
          #first check if exists some inactive UserCampaign for this user and campaign
          @user_campaign = UserCampaign.find(:first, :conditions=>"campaign_id=#{@campaign.id} AND user_id=#{@current_user.id}")
          if @user_campaign
            @user_campaign.is_active = true
            @user_campaign.save
          else
            @user_campaign = UserCampaign.new
            @user_campaign.user_id = @current_user.id
            @user_campaign.campaign_id = @campaign.id
            @user_campaign.is_active = true
            @user_campaign.save!        
          end         
        end
        
      else
        flash_error(:campaign_accept_terms)
        render :join_page
      end
    else
      flash_error(:campaign_cannot_joint_inactive)
      redirect_to campaigns_url
    end
  rescue ActiveRecord::ActiveRecordError
    flash_error(:campaign_requires_email)
    render :join_page
    
    
    
  end
  
end
